{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "242c7294",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "676b4f63",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自定义数据集类\n",
    "class SimpleDataset(Dataset):\n",
    "    def __init__(self):\n",
    "        self.data = [torch.arange(1).float() for _ in range(10)]\n",
    "        self.labels = torch.randint(0, 2, (10,))\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.data)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        return self.data[idx], self.labels[idx]\n",
    "\n",
    "# 创建数据集对象\n",
    "dataset = SimpleDataset()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a23ba427",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0.]), tensor(0))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "02eb9966",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 自定义数据集类\n",
    "class SimpleDataset(Dataset):\n",
    "    def __init__(self):\n",
    "        self.data = torch.arange(10).float().unsqueeze(1)\n",
    "        self.labels = torch.randint(0, 2, (10,))\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.data)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        return self.data[idx], self.labels[idx]\n",
    "\n",
    "# 创建数据集对象\n",
    "dataset = SimpleDataset()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e6a0fd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(tensor([0.]), tensor(0))\n",
      "(tensor([1.]), tensor(0))\n",
      "(tensor([2.]), tensor(0))\n",
      "(tensor([3.]), tensor(0))\n",
      "(tensor([4.]), tensor(0))\n",
      "(tensor([5.]), tensor(1))\n",
      "(tensor([6.]), tensor(0))\n",
      "(tensor([7.]), tensor(1))\n",
      "(tensor([8.]), tensor(1))\n",
      "(tensor([9.]), tensor(0))\n"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
      "\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
      "\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "for _ in dataset:\n",
    "    print(_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e9de679",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
